// import Taro from '@tarojs/taro';
import http from "@utils/http";
import http1 from "@utils/http1";
import { IMG_ADMIN, HOST } from "@config";

// 获取首页各个模块的栏目
export function getMarketingList(params) {
  return new Promise((resolve, reject) => {
    http1
      .request(
        {
          url: "/index/findMarketingList.json",
          data: params,
        },
        false
      )
      .then(({ data: { marketingDTOList } }) => {
        resolve(marketingDTOList.length ? marketingDTOList : []);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

// 获取首页各个模块的商品列表
export function getMarketGoodsList(params) {
  return new Promise((resolve, reject) => {
    http1
      .request(
        {
          url: "/index/selectMarketingGoodsByMarketingid.json",
          data: params,
        },
        false
      )
      .then(({ data: { marketingGoodsDTOList } }) => {
        resolve(marketingGoodsDTOList.length ? marketingGoodsDTOList : []);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

// 获取首页广告模块，如轮播图，中部广告区
export function getAdList(params) {
  return new Promise((resolve, reject) => {
    http1
      .request(
        {
          url: "/index/adList.json",
          data: params,
        },
        false
      )
      .then(({ data: { adList } }) => {
        resolve(adList.length ? adList : []);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

// 搜索商品
export function searchGoods(params) {
  return new Promise((resolve, reject) => {
    http
      .request(
        {
          url: "/index/searchGoods.json",
          data: params,
        },
        false
      )
      .then(({ data: { goodsInfoDTOList } }) => {
        resolve(goodsInfoDTOList.length ? goodsInfoDTOList : []);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

// 上传图片
// export function uploadImg(filePath) {
//   console.log(filePath)
//   return new Promise((resolve, reject) => {
//     Taro.uploadFile({
//       url: `${HOST}/common/uploadImage.json`,
//       filePath,
//       name: 'file',
//     }).then(({ data }) => {
//       console.log(data)
//       if (data) {
//         const { data: { url } } = JSON.parse(data)
//         resolve({
//           url: `${IMG_ADMIN}${url}`,
//           path: url
//         })
//       }
//     }).catch(err => {
//       reject(err)
//     })
//   })
// }

// 订单列表
export function getOrderList(params) {
  return new Promise((resolve, reject) => {
    http
      .request(
        {
          url: "/order/orderList.json",
          data: params,
        },
        true
      )
      .then(({ data }) => {
        const { orderInfoDTOList } = data || {};
        resolve(
          orderInfoDTOList && orderInfoDTOList.length ? orderInfoDTOList : []
        );
      })
      .catch((err) => {
        reject(err);
      });
  });
}

// 支付信息
export function getPayData() {
  return new Promise((resolve, reject) => {
    http
      .request(
        {
          url: "/user/getEnjoyPay.json",
          data: {},
        },
        true
      )
      .then(({ data }) => {
        const { enjoyPay } = data;
        resolve(enjoyPay || {});
      })
      .catch((err) => {
        reject(err);
      });
  });
}

// 订单详情
export function getOrderDetail(orderId) {
  return new Promise((resolve, reject) => {
    http
      .request(
        {
          url: `/order/orderDetail.json?orderId=${orderId}`,
          data: {
            orderId: Number(orderId),
          },
        },
        true
      )
      .then(({ data }) => {
        const { orderDetailDTO } = data;
        resolve(orderDetailDTO);
      })
      .catch((err) => {
        reject(err);
      });
  });
}
